using System;
using System.Collections.Generic;
using System.Reflection;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.OracleClient;

using Kenly.DBFramework.Reflection;

namespace Kenly.DBFramework.Gateway
{
    /// <summary>
    /// 
    /// </summary>
    internal class OracleParameterBuilder : ParameterBuilderBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnAttribute"></param>
        /// <param name="parameterName"></param>
        /// <param name="parameterValue"></param>
        /// <returns></returns>
        public override IDataParameter CreateColumnParameter(ColumnAttribute columnAttribute, string parameterName, object parameterValue)
        {
            parameterValue=AdaptParameterValue(parameterValue);
            bool isNullable = true;

            OracleParameter tempOracleParameter = ((parameterValue is DBNull) || parameterValue.GetType()==typeof(OracleCursor))?
                new OracleParameter(parameterName, columnAttribute.OracleType) : 
                new OracleParameter(parameterName, parameterValue);

            ResetOracleStringType(columnAttribute, parameterValue, tempOracleParameter);

            IDataParameter parameter = new OracleParameter(tempOracleParameter.ParameterName, tempOracleParameter.OracleType,
                columnAttribute.Size, tempOracleParameter.Direction,
                        isNullable, 0, 0, tempOracleParameter.SourceColumn, tempOracleParameter.SourceVersion, parameterValue);

            return parameter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnAttribute"></param>
        /// <param name="parameterValue"></param>
        /// <param name="tempOracleParameter"></param>
        private static void ResetOracleStringType(ColumnAttribute columnAttribute, object parameterValue, OracleParameter tempOracleParameter)
        {
            if (parameterValue != null && !(parameterValue is DBNull) && 
                parameterValue.GetType() == typeof(string))
            {
                if (columnAttribute.OracleType != ColumnAttribute.DefaultOracleType)
                {
                    tempOracleParameter.OracleType = columnAttribute.OracleType;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameterAttribute"></param>
        /// <param name="parameterName"></param>
        /// <param name="parameterValue"></param>
        /// <returns></returns>
        public override IDataParameter CreateProcedureParameter(ParameterAttribute parameterAttribute, string parameterName, object parameterValue)
        {
            parameterValue=AdaptParameterValue(parameterValue);
            bool isNullable = true;
            OracleParameter tempParameter = (parameterValue is DBNull) ?
                new OracleParameter(parameterName, parameterAttribute.OracleType) : new OracleParameter(parameterName, parameterValue);

            IDataParameter parameter = new OracleParameter(tempParameter.ParameterName, tempParameter.OracleType,
                parameterAttribute.Size, parameterAttribute.Direction,
                        isNullable, 0, 0, tempParameter.SourceColumn, tempParameter.SourceVersion, parameterValue);

            return parameter;
        }

      

    }


}
